//
//  HProgressView.m
//  Join
//
//  Created by 黄克瑾 on 2017/2/2.
//  Copyright © 2017年 huangkejin. All rights reserved.
//

#import "HProgressView.h"

@interface HProgressView ()


@end

@implementation HProgressView


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    
    self.layer.cornerRadius = self.width / 2.0;
    self.clipsToBounds = YES;
    // Drawing code
    CGContextRef ctx = UIGraphicsGetCurrentContext();//获取上下文
    CGPoint center = CGPointMake(self.frame.size.width/2.0, self.frame.size.width/2.0);  //设置圆心位置
    CGFloat radius = self.frame.size.width/2.0;  //设置半径
    CGFloat startA = - M_PI_2;  //圆起点位置
    CGFloat endA = -M_PI_2 + M_PI * 2 * _progressValue;  //圆终点位置
    
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startA endAngle:endA clockwise:YES];
    
    if (self.lineWidth == 0) {
        self.lineWidth = self.width / 2.0;
    }
    
    CGContextSetLineWidth(ctx, self.lineWidth * 2); //设置线条宽度
    [self.color setStroke]; //设置描边颜色
    
    CGContextAddPath(ctx, path.CGPath); //把路径添加到上下文
    
    CGContextStrokePath(ctx);  //渲染
}

//- (void)setTimeMax:(NSInteger)timeMax {
//    _timeMax = timeMax;
////    self.currentTime = 0;
//    self.progressValue = 0;
//    [self setNeedsDisplay];
//    self.hidden = NO;
//    [self performSelector:@selector(startProgress) withObject:nil afterDelay:0.1];
//}

- (void)clearProgress {
//    _currentTime = _timeMax;
    self.hidden = YES;
}

- (void)setProgressValue:(CGFloat)progressValue{
    _progressValue = progressValue;
    [self startProgress];
}

- (void)startProgress {
    if (_progressValue >= 1) {
        [self clearProgress];
    }else {
        [self setNeedsDisplay];
    }
}

@end
